package com.example.schedule.today;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.schedule.R;
import com.example.schedule.adapter.TodayPlanAdapter;
import com.example.schedule.db.Plan;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.litepal.LitePal;

import java.util.List;

public class NoteActivity extends AppCompatActivity {

    private FloatingActionButton mBtnTodayAdd;
    private Button mBtnTodayBack;
    private RecyclerView mTodayRvView;
    public static TodayPlanAdapter sTodayPlanAdapter;
    private String TAG = "yesen";
    private List<Plan> mPlanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate: ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note);
        initView();
        initEvent();
        initData();
    }

    private void initData() {
        mPlanList = LitePal.findAll(Plan.class);
        mTodayRvView = findViewById(R.id.today_list);
        sTodayPlanAdapter = new TodayPlanAdapter(mPlanList);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        //sTodayPlanAdapter.notifyDataSetChanged();
        mTodayRvView.setLayoutManager(manager);
        mTodayRvView.setAdapter(sTodayPlanAdapter);

    }

    private void initEvent() {
     mBtnTodayBack.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            finish();
         }
     });

     mBtnTodayAdd.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             Intent intent = new Intent(NoteActivity.this , TodayEdit.class);
             mBtnTodayAdd.setVisibility(View.GONE);
             startActivity(intent);
         }
     });
    }

    private void initView() {
        mBtnTodayBack = findViewById(R.id.btn_today_back);
        mBtnTodayAdd = findViewById(R.id.btn_today_add);

    }

    @Override
    protected void onResume() {
        //setContentView(R.layout.activity_note);
        Log.d(TAG, "onResume: ");
        super.onResume();
        mBtnTodayAdd.setVisibility(View.VISIBLE);
        refreshDB();
    }

    private void refreshDB() {
        mPlanList = LitePal.findAll(Plan.class);
        Log.d(TAG, "refreshDB: "+mPlanList.size());
        sTodayPlanAdapter.refreshData(mPlanList);
    }

    @Override
    protected void onStart() {
        Log.d(TAG, "onStart: ");
        super.onStart();
    }

    @Override
    protected void onStop() {
        Log.d(TAG, "onStop: ");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "onDestroy: ");
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        Log.d(TAG, "onPause: ");
        super.onPause();
    }
}